//
//  XFPictureCell.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/25.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFPictureViewCell.h"

@interface XFPictureViewCell ()

@property(nonatomic, weak) UIButton *picBtn;
@property(nonatomic, weak) UIButton *deleBtn;

@end

@implementation XFPictureViewCell

- (void)setPicture:(UIImage *)picture {

  _picture = picture;
  self.deleBtn.hidden = (picture == nil);
  if (picture != nil) {
    [self.picBtn setBackgroundImage:picture forState:UIControlStateNormal];
  } else {
    [self.picBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"]
                           forState:UIControlStateNormal];
  }
}

// 1.重写 init 方法

- (instancetype)initWithFrame:(CGRect)frame {

  if (self = [super initWithFrame:frame]) {
    [self setupUI];
    [self setupFrame];
  }
  return self;
}

- (void)addPictureDidClick {

  if ([self.delegate
          respondsToSelector:@selector(addPictureWithpictureViewCell:)]) {
    [self.delegate addPictureWithpictureViewCell:self];
  }
}

- (void)deletePictureDidClick {

  if ([self.delegate
          respondsToSelector:@selector(deletePictureWithpictureViewCell:)]) {
    [self.delegate deletePictureWithpictureViewCell:self];
  }
}

// 2. setupUI添加子控件
- (void)setupUI {

  UIButton *button = [[UIButton alloc] init];
  [button setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"]
                    forState:UIControlStateNormal];
  button.highlighted = NO;
  [self.contentView addSubview:button];
  [button addTarget:self
                action:@selector(addPictureDidClick)
      forControlEvents:UIControlEventTouchUpInside];
  self.picBtn = button;

  UIButton *deleteBtn = [[UIButton alloc] init];
  [deleteBtn setBackgroundImage:[UIImage imageNamed:@"compose_photo_close"]
                       forState:UIControlStateNormal];
  [deleteBtn
      setBackgroundImage:[UIImage imageNamed:@"compose_photo_close_highlighted"]
                forState:UIControlStateHighlighted];
  [self.contentView addSubview:deleteBtn];
  self.deleBtn = deleteBtn;
  [deleteBtn addTarget:self
                action:@selector(deletePictureDidClick)
      forControlEvents:UIControlEventTouchUpInside];
}

// 3. setupFrame添加子控件约束
- (void)setupFrame {

  [self.picBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.top.bottom.equalTo(self.contentView);
  }];

  [self.deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView.mas_top);
    make.right.equalTo(self.contentView.mas_right);
  }];
}

@end
